<template>
  <div>
    <el-card style="width: 40%; margin: 10px">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="序号">
          <el-input v-model="form.id" disabled></el-input>
        </el-form-item>

        <el-form-item label="昵称">
          <el-input v-model="form.username" ></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="form.age"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="form.sex" label="男">男</el-radio>
          <el-radio v-model="form.sex" label="女">女</el-radio>
          <el-radio v-model="form.sex" label="未知">未知</el-radio>
        </el-form-item>
        <el-form-item label="出生日期">
          <el-input v-model="form.birth"></el-input>
        </el-form-item>
        <el-form-item label="死亡日期">
          <el-radio v-model="form.marry" label="存活">存活</el-radio>
          <el-input type="textarea" v-model="form.marry" style="width:80%;"></el-input>
        </el-form-item>

        <el-form-item label="家族序号">
          <el-input v-model="form.fid" ></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <el-upload
              action="http://localhost:9090/files/upload" :on-success="fileUploadSucess"
          >
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>

      </el-form>
      <div style="text-align: center">
        <el-button type="primary" @click="update">保存</el-button>
      </div>
    </el-card>

  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Person",
  data() {
    return {
      form: {}
    }
  },
  created() {
    let str = sessionStorage.getItem("user") || "{}"
    this.form = JSON.parse(str)
  },
  methods: {
    fileUploadSucess(res){
      console.log(res)
      this.form.cover=res.data
    },
    update() {
      request.put("/user", this.form).then(res => {
        console.log(res)
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: "更新成功"
          })
          sessionStorage.setItem("user", JSON.stringify(this.form))
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>